package com.easepal7506a.project.ui.activity.activity_7506l;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.easepal7506a.project.Constant.CommmandNum;
import com.ogawa.base.Constant.Constants;
import com.easepal7506a.project.R;
import com.easepal7506a.project.interfaces.OnTimeChoose;
import com.easepal7506a.project.manager.DataManager;
import com.easepal7506a.project.observer.CloseDetailObserver;
import com.easepal7506a.project.observer.CloseProfessionalObserver;
import com.easepal7506a.project.observer.CloseScanAndAdjust;
import com.easepal7506a.project.observer.TimeObserver;
import com.easepal7506a.project.ui.activity.BaseActivity;
import com.easepal7506a.project.ui.iview.IProMasDetailView7506;
import com.easepal7506a.project.ui.presenter.ProMasDetailPresenter7506;
import com.easepal7506a.project.utils.ActivityUtils;
import com.easepal7506a.project.view.MoveLayout;
import com.easepal7506a.project.widget.LayoutBody7506L;
import com.easepal7506a.project.widget.LayoutFunctionView7506;
import com.easepal7506a.project.widget.LayoutGears5_7506;
import com.easepal7506a.project.widget.LayoutGears6_7506;
import com.ogawa.base.utils.CommonUtil;
import com.ogawa.base.utils.DownTimer;

import java.lang.ref.WeakReference;


/**
 * 专业按摩 按摩页面
 * Created by FHB on 2018/3/27.
 */

public class ProMasDetailActivity7506L extends BaseActivity implements View.OnClickListener,
        MoveLayout.OnChooseAction, OnTimeChoose ,IProMasDetailView7506 ,TimeObserver.onTimeUpdataLis, CloseDetailObserver.onCloseDetail {

    private RelativeLayout mLl3D;
    private RelativeLayout mLlStrong;
    private RelativeLayout mLlSpeed;
    private RelativeLayout mLlAir;
    private boolean isFinishShow;
    private ImageView mIvStart;
    private LayoutBody7506L mLayoutBody7506;
    private LayoutFunctionView7506 mLayoutFunction;
    private String mName;
    private ProMasDetailPresenter7506 mPresenter;


    private LayoutGears6_7506 mLayout3D;
    private LayoutGears5_7506 mLayoutStrong;
    private LayoutGears5_7506 mLayoutSpeed;
    private LayoutGears5_7506 mLayoutAir;
    private String[] name = {"3D", "强度", "速度", "气压强度"};
    private MoveLayout mLayout;
    private ScrollView mScroll;
    private TextView mTvTime;
    private MyHandler myHandler;
    //    private MyCountDownTimer timer;
    private final long INTERVAL = 1000L;
    private long mTime;
    private DownTimer mTimer;


    @Override
    public int setLayoutId() {
        return R.layout.activity_pro_massage_detail_7506l;
    }



    @Override
    public void initTitleBar() {
        super.initTitleBar();
        titleBar.setGreenBackBotton();
        titleBar.setTitle(mName);
        setBackGround(R.mipmap.bg);
        setmName("ProMasDetailActivity7506L");
    }

    @Override
    public void initView(int layoutId) {
        super.initView(layoutId);
        initView();
    }

    private void initView() {
        mName = getIntent().getStringExtra("name");
        setTimeListener(this);
        mLl3D = findViewById(R.id.layout_3d_7506);
        mLlStrong = findViewById(R.id.linear_strong_7506);
        mLlSpeed = findViewById(R.id.linear_speed_7506);
        mLlAir = findViewById(R.id.linear_strong_air_7506);
        mTvTime = findViewById(R.id.showTime_7506);
        mLayoutBody7506 = findViewById(R.id.layout_body_view_7506);
        mLayoutFunction = findViewById(R.id.activity_pro_mas_functionview_7506);
        mScroll = findViewById(R.id.activity_detail_scroll_7506);
        mLayout = findViewById(R.id.detail_move_layout_7506);
        mLayout.setData(name);
        mLayout.setScollView(mScroll);
        mLayout.setAction(this);
        mIvStart = findViewById(R.id.imgSwitch_7506);
        mLayout3D = findViewById(R.id.layout_3d_7506);
        mLayoutStrong = findViewById(R.id.linear_strong_7506);
        mLayoutSpeed = findViewById(R.id.linear_speed_7506);
        mLayoutAir = findViewById(R.id.linear_strong_air_7506);
        mLayout3D.setType(1);
        mLayoutStrong.setType(1);
        mLayoutSpeed.setType(2);
        mLayoutAir.setType(3);
        mIvStart.setSelected(DataManager.getInst().isPause());
        myHandler = new MyHandler(this);
        if(!TextUtils.isEmpty(DataManager.getInst().getValue(CommmandNum.mStatus3D))){
            Log.e("setset3d",DataManager.getInst().getValue(CommmandNum.mStatus3D)+"");
            mLayout3D.setShow(Integer.valueOf(DataManager.getInst().getValue(CommmandNum.mStatus3D)));
        }
        if(!TextUtils.isEmpty(DataManager.getInst().getValue(CommmandNum.mStatusStrength))){
            Log.e("setsetstring",DataManager.getInst().getValue(CommmandNum.mStatusStrength)+"");
            mLayoutStrong.setShow(Integer.valueOf(DataManager.getInst().getValue(CommmandNum.mStatusStrength)));
        }
        if(!TextUtils.isEmpty(DataManager.getInst().getValue(CommmandNum.mStatusSpeed))){
            Log.e("setsetspeed",DataManager.getInst().getValue(CommmandNum.mStatusSpeed)+"");
            mLayoutSpeed.setShow(Integer.valueOf(DataManager.getInst().getValue(CommmandNum.mStatusSpeed)));
        }
        if(!TextUtils.isEmpty(DataManager.getInst().getValue(CommmandNum.mStatusAirStrong))){
            Log.e("setsetair",DataManager.getInst().getValue(CommmandNum.mStatusAirStrong)+"");
            mLayoutAir.setShow(Integer.valueOf(DataManager.getInst().getValue(CommmandNum.mStatusAirStrong)));
        }
        mPresenter = new ProMasDetailPresenter7506(this);
        mPresenter.getTime();
        mTvTime.setOnClickListener(this);
        mIvStart.setOnClickListener(this);
        if(DataManager.getInst().getValue(CommmandNum.mStatusShoulderScanning).equals("1")){
            Log.e("detaill1",DataManager.getInst().getValue(CommmandNum.mStatusShoulderScanning)+"");
            ActivityUtils.startScanShoulderActivity(this);
        }else if(DataManager.getInst().getValue(CommmandNum.mStatusShoulderAdjust).equals("1")){
            ActivityUtils.startShouldersAdjustmentActivity(this);

        }
        TimeObserver.getInst().registerObserver(this);
        CloseDetailObserver.getInst().registerObserver(this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestory();
        mLayoutBody7506.onDestory();
        mLayoutFunction.onDestory();
        TimeObserver.getInst().removeObserver(this);
        CloseDetailObserver.getInst().removeObserver(this);
        cancelTime();
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.imgSwitch_7506) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                sendCommond(CommmandNum.PAUSE);

            }

        } else if (i == R.id.showTime_7506) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                showDialog();

            }

        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ActivityUtils.startProfessionMassaActivityL(this);
        this.finish();
    }

    @Override
    public void onChoose(int position) {
        mLayout.setTectColor(position);
        switch (position) {
            case 0:
                mLlStrong.setVisibility(View.GONE);
                mLl3D.setVisibility(View.VISIBLE);
                mLlSpeed.setVisibility(View.GONE);
                mLlAir.setVisibility(View.GONE);


                break;
            case 1:
                mLlStrong.setVisibility(View.VISIBLE);
                mLl3D.setVisibility(View.GONE);
                mLlSpeed.setVisibility(View.GONE);
                mLlAir.setVisibility(View.GONE);
                break;
            case 2:
                mLlStrong.setVisibility(View.GONE);
                mLl3D.setVisibility(View.GONE);
                mLlSpeed.setVisibility(View.VISIBLE);
                mLlAir.setVisibility(View.GONE);
                break;
            case 3:
                mLlStrong.setVisibility(View.GONE);
                mLl3D.setVisibility(View.GONE);
                mLlSpeed.setVisibility(View.GONE);
                mLlAir.setVisibility(View.VISIBLE);
                break;

        }

    }


    @Override
    public void onTimeListener(String time) {
//        mTvTime.setText(time+"");
        if(time.equals(Constants.TIME_5)){
            sendCommond(CommmandNum.TIME_5);

        }
        else if(time.equals(Constants.TIME_10)){
            sendCommond(CommmandNum.TIME_10);
        }
        else if(time.equals(Constants.TIME_15)){
            sendCommond(CommmandNum.TIME_15);
        }
        else if(time.equals(Constants.TIME_20)){
            sendCommond(CommmandNum.TIME_20);
        }
        else if(time.equals(Constants.TIME_25)){
            sendCommond(CommmandNum.TIME_25);
        }
        else if(time.equals(Constants.TIME_30)){
            sendCommond(CommmandNum.TIME_30);
        }

//        startTimer(Integer.valueOf(time.substring(0,time.indexOf(":")).trim()),Integer.valueOf(time.substring(time.length()-2,time.length() )));
    }

    @Override
    public void get3D(int position) {
        Log.e("getASSSSS+3d",position+"");
        mLayout3D.setShow(position);

    }

    @Override
    public void getAir(int position) {
        Log.e("getASSSSS+air",position+"");
        mLayoutAir.setShow(position);
    }

    @Override
    public void getStrength(int position) {
        Log.e("getASSSSS+stre",position+"");
        mLayoutStrong.setShow(position);
    }

    @Override
    public void getSpeed(int position) {
        Log.e("getASSSSS+speed",position+"");
        mLayoutSpeed.setShow(position);


    }

    @Override
    public void startToScan() {
        ActivityUtils.startScanShoulderActivity(this);

    }

    @Override
    public void startAdjust() {
        ActivityUtils.startShouldersAdjustmentActivity(this);
    }

    @Override
    public void closeActivity() {
        ActivityUtils.startHomeActivity(this);
        this.finish();

    }

    @Override
    public void startAdvance() {
        CloseProfessionalObserver.getInst().noticeonClose();
        CloseScanAndAdjust.getInst().noticeonClose();
        ActivityUtils.startAdvancedMassageActivity(this);
        this.finish();
    }

    @Override
    public void onupdataTime(String min, String sec) {
        if(!(min.equals("0")&&sec.equals("0"))){
            long time = (Integer.valueOf(min)*60+Integer.valueOf(sec));
            Log.e("updataTime_detail",time+"==="+mTime);
            if(Math.abs(time-mTime)>30){
                setTime(Integer.valueOf(min),Integer.valueOf(sec));
            }
        }else {

            Log.e("updataTime_detail222",min+"==="+mTime);
            mTvTime.setText("00 : 00");
        }

    }

    @Override
    public void onClose() {
        ActivityUtils.startProfessionMassaActivityL(this);
        this.finish();

    }


    private class MyHandler extends Handler {
        WeakReference<ProMasDetailActivity7506L> mProMasDetailActivityWeakReference;

        MyHandler(ProMasDetailActivity7506L mProMasDetailActivity) {
            mProMasDetailActivityWeakReference = new WeakReference<>(mProMasDetailActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            final ProMasDetailActivity7506L mHomeActivity = mProMasDetailActivityWeakReference.get();
            if (mHomeActivity != null) {
                switch (msg.what) {
                    case 1:
                        mLayout3D.setShow((int) msg.obj);
                        break;


                }
            }
        }
    }



    @Override
    public void getTime(String min, String sec) {
        if(!DataManager.getInst().isPause()){
            setTime(Integer.valueOf(min.trim()),Integer.valueOf(sec.trim()));
        }

    }

    @Override
    public void getName(String name) {
        Log.e("toProMasDetailActivity2",name+"");
        mName = name;
        titleBar.setTitle(name);
    }

    @Override
    public void isPause(boolean isPause) {
        mIvStart.setSelected(isPause);
        if(isPause){
            pauseTime();
        }
        else {
            reStarTime();
        }
    }



    public void pauseTime(){
        if(mTimer!=null){
            mTimer.pause();
        }

    }
    public void reStarTime(){
        if(mTimer!=null){
            mTimer.resume();
        }


    }
    public void cancelTime(){
        if(mTimer!=null){
            mTimer.cancel();
        }


    }

    public void setTime(int min,int sec){
        cancelTime();
        if(DataManager.getInst().isAdvanceRunning()||DataManager.getInst().isAutoRunning()){
            mTimer = new DownTimer();//实例化
            mTimer.setTotalTime(min*60*1000+sec*1000);//设置毫秒数
            mTimer.setIntervalTime(1000);//设置间隔数
            mTimer.setTimerLiener(new DownTimer.TimeListener() {
                @Override
                public void onFinish() {
                    mTvTime.setText("00 : 00");
                    sendCommond(CommmandNum.FINISH);
                    if(!ProMasDetailActivity7506L.this.isFinishing()){
                        if(!isFinishShow){
                            isFinishShow = true;
                            showFinishDialog(ProMasDetailActivity7506L.this,"1", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    isFinishShow = false;
                                    cancleFinishDialog();
                                    ActivityUtils.startHomeActivity(ProMasDetailActivity7506L.this);
                                }
                            });
                        }
                    }

                }
                @Override
                public void onInterval(long time) {
                    long showTime =time/1000;
                    mTime = showTime;
                    if (showTime <= 59) {
                        mTvTime.setText(String.format("00:%02d", showTime));
                    } else {
                        mTvTime.setText(String.format("%02d:%02d", showTime / 60, showTime % 60));
                    }
                }
            });
            mTimer.start();
        }else {
            mTvTime.setText("00 : 00");
        }

    }
}
